package com.acwing.partition9;

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;

/**
 * @author `RKC`
 * @date 2021/12/1 15:05
 */
public class AC859Kruskal算法求最小生成树 {

    private static final int INF = 0x3F3F3F3F;

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s = reader.readLine().split(" ");
        int n = Integer.parseInt(s[0]), m = Integer.parseInt(s[1]);
        int[][] edges = new int[m][3];
        for (int i = 0; i < m; i++) {
            s = reader.readLine().split(" ");
            int x = Integer.parseInt(s[0]), y = Integer.parseInt(s[1]), z = Integer.parseInt(s[2]);
            edges[i][0] = x;
            edges[i][1] = y;
            edges[i][2] = z;
        }
        int ans = kruskal(n, edges);
        if (ans == INF) writer.write("impossible\n");
        else writer.write(ans + "\n");
        writer.flush();
    }

    private static int kruskal(int n, int[][] edges) {
        //初始化并查集
        int[] ancestors = new int[n + 1];
        for (int i = 1; i <= n; i++) ancestors[i] = i;
        //对边进行排序
        Arrays.sort(edges, Comparator.comparingInt(o -> o[2]));
        int minEdgeCount = 0, answer = 0;
        for (int[] edge : edges) {
            if (minEdgeCount == n - 1) break;
            if (find(ancestors, edge[0]) != find(ancestors, edge[1])) {
                union(ancestors, edge[1], edge[0]);
                answer += edge[2];
                minEdgeCount++;
            }
        }
        if (minEdgeCount != n - 1) return INF;
        return answer;
    }

    private static void union(int[] ancestors, int x, int y) {
        int a = find(ancestors, x), b = find(ancestors, y);
        if (a != b) ancestors[b] = ancestors[a];
    }

    private static int find(int[] ancestors, int x) {
        if (x == ancestors[x]) return x;
        ancestors[x] = find(ancestors, ancestors[x]);
        return ancestors[x];
    }
}
